package cn.harry.cabinet.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import cn.harry.cabinet.model.entity.UsageRecord

/**
 * 使用记录表数据访问对象
 */
@Dao
interface UsageRecordDao {
    
    @Query("SELECT * FROM usage_records")
    fun getAllUsageRecords(): List<UsageRecord>
    
    @Query("SELECT * FROM usage_records WHERE id = :id")
    fun getUsageRecordById(id: Long): UsageRecord?
    
    @Query("SELECT * FROM usage_records WHERE status = :status")
    fun getUsageRecordsByStatus(status: cn.harry.cabinet.model.enums.OperationType): List<UsageRecord>
    
    @Query("SELECT * FROM usage_records WHERE cardNumber = :cardNumber AND status = :status AND returnTime IS NULL ORDER BY takeTime DESC LIMIT 1")
    fun getUnreturnedRecordByCardNumber(cardNumber: String, status: cn.harry.cabinet.model.enums.OperationType = cn.harry.cabinet.model.enums.OperationType.RETRIEVE): UsageRecord?
    
    @Insert
    fun insertUsageRecord(usageRecord: UsageRecord): Long
    
    @Update
    fun updateUsageRecord(usageRecord: UsageRecord)
    
    @Delete
    fun deleteUsageRecord(usageRecord: UsageRecord)
    
    @Query("DELETE FROM usage_records")
    fun deleteAllUsageRecords()
}